﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using SdmxMl.Helper;
using SdmxMl.Common;

namespace SdmxMl.Structure
{
    /// <summary>
    /// MetadataStructureRefType provides a reference to a metadata structure definition.
    /// The ID must be provided, as assigned to the metadata structure definition by the
    /// agency whose ID is the value of MetadataStructureAgencyID. A version must also be provided.
    /// </summary>
    public class MetadataStructureRefType: ArtefactRefBase
    {
        #region Construction

        public MetadataStructureRefType()
        {
        }

        public MetadataStructureRefType(XmlNode nodeArtefact, XmlNamespaceManager nsmgr)
        {
            Id = Xmlhelper.GetNodeText(nodeArtefact, "./structure:MetadataStructureID", nsmgr);
            AgencyId = Xmlhelper.GetNodeText(nodeArtefact, "./structure:MetadataStructureAgencyID", nsmgr);
            Version = Xmlhelper.GetNodeText(nodeArtefact, "./structure:Version", nsmgr);

            Urn = Xmlhelper.GetNodeText(nodeArtefact, "./structure:URN", nsmgr);
        }

        #endregion //Construction

        #region XML Representation

        public void WriteXml(XmlTextWriter xw, string prefix, string tag)
        {
            // Nothing to do if not specified
            if (string.IsNullOrEmpty(Id) && string.IsNullOrEmpty(Urn))
                return;

            xw.WriteStartElement(prefix, tag, null);
            
            if (string.IsNullOrEmpty(Urn))
            {
                Xmlhelper.WriteElementIfExist(xw, prefix, "MetadataStructureID", Id);
                Xmlhelper.WriteElementIfExist(xw, prefix, "MetadataStructureAgencyID", AgencyId);
                Xmlhelper.WriteElementIfExist(xw, prefix, "Version", Version);
            }
            else
                Xmlhelper.WriteElementIfExist(xw, prefix, "URN", Urn);
            
            xw.WriteEndElement();
        }

        #endregion //XMl Representation

        #region ISdmxArtefactRef Members

        #endregion //ISdmxArtefactRef Members

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(Id).Append(" - V.").Append(Version).Append(" - ").Append(AgencyId);
            return sb.ToString(); 
        }
    }


    public class MetadataStructureRefTypeList : BaseSetlistRefList
    {
    }
}
